package io.github.biezhi.java8.completablefuture;

import java.util.concurrent.*;

/**
 * @author biezhi
 * @date 2018/3/25
 */
public class CompletableFuture10 {


    private static final ThreadPoolExecutor threadPoolExecutor =
            new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2,
                    Runtime.getRuntime().availableProcessors() * 3, 10,
                    TimeUnit.MINUTES,
                    new LinkedBlockingQueue<>(1000));
    private static CompletableFuture<Integer> m1() {
        return CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 2333;
        }, threadPoolExecutor);
    }

    private static CompletableFuture<Integer> m2() {
        return CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 8877;
        }, threadPoolExecutor);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long start = System.currentTimeMillis();
        CompletableFuture.anyOf(m1(), m2()).thenRun(() -> {
            System.out.println(System.currentTimeMillis() - start);
        }).get();
    }
}
